import crypto from 'crypto'
//加密密码
export function getmd5(pass) {
    let md5 = crypto.createHash("md5");
    md5.update(pass);
    let a = md5.digest('hex').toUpperCase();
    // console.log(a,'加密');
    return a;
}
//生成uuid
export function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
//验证手机号
export function yzMobile(val) {
    if (/^1[3|4|5|8][0-9]\d{4,8}$/.test(val)) {
        return true
    } else {
        return false;
    }
}
//验证验证码
export function yzVerify(val) {
    if (/^\d{4,8}$/.test(val)) {
        return true
    } else {
        return false;
    }
}
//验证密码
export function yzPwd(val) {
    if (/^\w{6,16}$/.test(val)) {
        return true
    } else {
        return false;
    }
}
//验证用户名
export function yzName(val) {
    if (/^\S+$/.test(val)) {
        return true
    } else {
        return false;
    }
}

//
export function getHeaders(_token = '') {
    let headers = {}
    let len = 6
    let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    let maxPos = chars.length
    let nonceStr = ''
    for (let i = 0; i < len; i++) {
        nonceStr += chars.charAt(Math.floor(Math.random() * maxPos))
    }
    let token = '';
    if (_token) {
        token = _token;
    } else {
        token = '';
        // token = '35f70407d597aa09907fe973c52d562b';
    }
    let query = "app_id=f1b396f5f8ff6adf&app_key=32001050f1b396f5f8ff6adf92c5c68b&random=" + nonceStr // app_id加上app_key上面拼好的字符串
        // let db_token = getmd5(token).toUpperCase() // 得到数据TOKEN并转大写
    let sign = getmd5(query).toUpperCase() // 生成签名并转大写

    headers.Authorization = 'Bearer ' + token; // 设置 axios 默认请求头
    //headers.Access-Control-Allow-Origin = '*.*'
    //headers.sign = sign; // 设置 axios 默认请求头
    //headers.random = nonceStr; // 设置 axios 默认请求头
    //headers.timestamp = (new Date()).valueOf();
    return headers
}

//验证网站地址
export function checkHttp(val) {
    var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
    if (reg.test(val)) {
        return true
    } else {
        return false;
    }
}

//验证是否整数（）
export function isIntNum(val, type = 0) {
    if ((val === '' || val == undefined) && val !== 0) {
        return true;
    }
    var regPos = / ^\d+$/; // 非负整数
    var regNeg = /^\-[1-9][0-9]*$/; // 负整数
    if (type == 1) {
        if (!regPos.test(val) || !regNeg.test(val)) {
            return false;
        } else {
            return true;
        }
    } else {
        if (!regPos.test(val)) {
            return false;
        } else {
            return true;
        }
    }
}

//获取单条数据链
export function getCheckedNodeAndParents(array, checkedNodeAndParents, curNodeId) {
    for(let i = 0; i < array.length; i++) {
        const item = array[i];
        if (item.id === curNodeId) {
            checkedNodeAndParents.push(item);
            return checkedNodeAndParents;
        }
        if(item.children && item.children.length > 0) {
            checkedNodeAndParents.push(item);
            const rs = getCheckedNodeAndParents(item.children, checkedNodeAndParents, curNodeId);
            if(rs) {
                return rs;
            } else {
                checkedNodeAndParents.remove(item);
            }
        }
    }
    //Array扩展remove方法
    Array.prototype.remove = function (val) {
        const index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };
    return false;
}
